import pygame
import main

mario_0 = pygame.image.load(r'Img/mario_00.png')
mario_run_1 = pygame.image.load(r'Img/mario_run_01.png')
mario_run_2 = pygame.image.load(r"Img/mario_run_02.png")
mario_run_3 = pygame.image.load(r"Img/mario_run_03.png")
mario_0_left = pygame.transform.flip(mario_0, True, False)
mario_run_left_1 = pygame.transform.flip(mario_run_1, True, False)
mario_run_left_2 = pygame.transform.flip(mario_run_2, True, False)
mario_run_left_3 = pygame.transform.flip(mario_run_3, True, False)
mario_jump_1 = pygame.image.load(r"Img/mario_jump_01.png")
mario_jump_2 = pygame.image.load(r"Img/mario_jump_02.png")
mario_death = pygame.image.load(r"Img/mario_death.png")
img_background = pygame.image.load(r"Img/level_1.png")


class Mario(pygame.sprite.Sprite):
    def __init__(self, pos):
        pygame.sprite.Sprite.__init__(self)
        self.hp = 3
        self.jump_num = 0
        self.gravity = 2.0
        self.antigravity = 0.3
        self.image = pygame.image.load(r"Img/mario_00.png").convert_alpha()
        self.image = pygame.transform.rotozoom(self.image, 0, 0.5)
        self.width, self.height = self.image.get_size()
        self.rect = self.image.get_rect()
        self.rect.left, self.rect.top = pos
        self.rect.top = self.rect.top - self.height
        self.right = self.rect.left + self.width  # 图片右坐标
        self.speed = 10
        self.min_y = 0
        self.death_jump_num = 0
        self.death_jump_down = False
        self.is_death = False
        self.is_pipe = False
        self.vel_y = 0
        self.is_jump = False
        self.is_down = False
        # 跑动时的动画
        self.run_sprites = []
        self.run_sprites.append(pygame.transform.scale(mario_0, (38, 37)))
        self.run_sprites.append(pygame.transform.scale(mario_run_1, (38, 37)))
        self.run_sprites.append(pygame.transform.scale(mario_run_2, (38, 37)))
        self.run_sprites.append(pygame.transform.scale(mario_run_3, (38, 37)))
        self.run_current = 0
        # 向左奔跑时的动画
        self.run_sprites_left = []
        self.run_sprites_left.append(pygame.transform.scale(mario_0_left, (38, 37)))
        self.run_sprites_left.append(pygame.transform.scale(mario_run_left_1, (38, 37)))
        self.run_sprites_left.append(pygame.transform.scale(mario_run_left_2, (38, 37)))
        self.run_sprites_left.append(pygame.transform.scale(mario_run_left_3, (38, 37)))
        self.run_current_left = 0

    def mario_jump(self, bg_size, screen):
        move_y = 0
        key = pygame.key.get_pressed()
        if key[pygame.K_SPACE] and self.is_jump is False and key[pygame.K_a]:
            jump_sound = pygame.mixer.Sound("sound/small_jump.ogg")  # 添加跳跃音效
            jump_sound.play()
            self.image = pygame.transform.flip(mario_jump_2, True, False)
            self.image = pygame.transform.scale(self.image, (38, 37))
            self.vel_y = -18
            self.is_jump = True
        if key[pygame.K_SPACE] and self.is_jump is False and key[pygame.K_d]:
            jump_sound = pygame.mixer.Sound("sound/small_jump.ogg")
            jump_sound.play()
            self.image = pygame.transform.scale(mario_jump_2, (38, 37))
            self.vel_y = -18
            self.is_jump = True
        if key[pygame.K_SPACE] and self.is_jump is False:
            jump_sound = pygame.mixer.Sound("sound/small_jump.ogg")
            jump_sound.play()
            self.image = pygame.transform.scale(mario_jump_2, (38, 37))
            self.vel_y = -18
            self.is_jump = True
        if key[pygame.K_a] and self.is_jump is False:
            self.mario_left()
            screen.blit(img_background, (0, 0))
        elif key[pygame.K_a] and self.is_jump is True:
            self.rect.left -= self.speed
        if key[pygame.K_d] and self.is_jump is False:
            self.mario_right()
            screen.blit(img_background, (0, 0))
        elif key[pygame.K_d] and self.is_jump is True:
            self.rect.left += self.speed

        # 控制跳跃高度
        self.vel_y += 1.2
        if self.vel_y > 10:
            self.vel_y = 10
        move_y += self.vel_y
        self.rect.top += move_y

        # 控制人物的最低位置
        if self.rect.bottom > bg_size - 44 and self.is_pipe is False and self.is_down is False:
            self.rect.bottom = bg_size - 44
            self.is_jump = False
        if self.rect.bottom > self.min_y and self.is_pipe is True:
            self.rect.bottom = self.min_y
            self.is_jump = False

    def mario_left(self):
        self.run_current_left += 1
        self.rect.left -= self.speed
        if self.run_current_left >= len(self.run_sprites_left):
            self.run_current_left = 0
        self.image = self.run_sprites_left[self.run_current_left]

    def mario_right(self):
        self.run_current += 1
        self.rect.left += self.speed
        if self.run_current >= len(self.run_sprites):
            self.run_current = 0
        self.image = self.run_sprites[self.run_current]

    def mario_death(self):
        self.image = pygame.transform.scale(mario_death, (38, 37))
        death_sound = pygame.mixer.Sound("music/death.wav")
        death_sound.set_volume(0.2)
        pygame.mixer.music.stop()
        death_sound.play()
        if self.death_jump_num < 50 and self.death_jump_down is False:
            self.death_jump_num += 10
            self.rect.y -= self.death_jump_num
        else:
            if self.death_jump_num > 0:
                self.death_jump_down = True
                self.death_jump_num -= 10
                self.rect.y -= self.death_jump_num
